//package com.rbac.service.test.impl;
//
//import com.common.Result;
//import com.domain.dto.test.RolePermissionDto;
//import com.domain.vo.test.PermissionTreeVo;
//import com.rbac.exception.CommonException;
//import com.rbac.mapper.test.PermissionMapper2;
//import com.rbac.mapper.test.RoleMapper2;
//import com.rbac.pojo.Permission;
//import com.rbac.service.test.PermissionService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.ApplicationContext;
//import org.springframework.stereotype.Service;
//import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
//
//import java.util.ArrayList;
//import java.util.List;
//import java.util.Objects;
//import java.util.stream.Collectors;
//
//@Service
//public class PermissionServiceImpl implements PermissionService {
//
//    @Autowired
//    private PermissionMapper2 permissionMapper2;
//    @Autowired
//    private RoleMapper2 roleMapper2;
//    @Autowired
//    private ApplicationContext applicationContext;
//    @Autowired
//    private RequestMappingHandlerMapping requestMappingHandlerMapping;
//
//
//    @Override
//    public List<Permission> getTreeMenu(Integer userId) {
//
//        List<Permission> permissions = getUserPermission(userId);
//        //构建一级菜单
//        List<Permission> parentList = permissions.stream()
//                .filter(item -> item.getParentId() == 0)
//                .collect(Collectors.toList());
//        System.out.println(parentList);
//
//        //递归构建菜单树
//        buildMenuTree(parentList, permissions);
////
////        //构造一级菜单
////        for (Permission permission : parentList) {
////            List<Permission> ChildrenList = permissions.stream().filter(item -> permission.getId().equals(item.getParentId())).collect(Collectors.toList());
////        }
////
//        System.out.println(parentList);
//        return parentList;
//    }
//
//    @Override
//    public Result getPermissionListAll() {
//
//        //获取全部数据
//        List<Permission> permissions = permissionMapper2.getListAll();
////        System.out.println(permissions);
//
//        List<PermissionTreeVo> permissionTreeVos = new ArrayList<>();
//        for(Permission permission : permissions){
//            PermissionTreeVo permissionTreeVo = new PermissionTreeVo();
//            permissionTreeVo.setId(permission.getId());
//            permissionTreeVo.setLabel(permission.getName());
//            permissionTreeVo.setParentId(permission.getParentId());
//            permissionTreeVos.add(permissionTreeVo);
//        };
////        System.out.println(permissionTreeVos);
//
//        //构建权限树状列表
//        List<PermissionTreeVo> parentList = permissionTreeVos.stream()
//                .filter(item -> item.getParentId() == 0)
//                .collect(Collectors.toList());
//
////        System.out.println("parentList"+parentList);
//        buildPermissionTree(parentList, permissionTreeVos);
//
//        if(parentList.isEmpty()){
//            throw new CommonException("查询权限列表为空");
//        }
//        return Result.success(parentList);
//    }
//
//    @Override
//    public List<Permission> getUserPermissionByRoleId(Integer roleId) {
//
//        return permissionMapper2.getListByRoleId(roleId);
//    }
//
//    @Override
//    public Result updateRolePermission(RolePermissionDto rolePermissionDto) {
//        Integer roleId = rolePermissionDto.getId();
//        List<Integer> rolePermissionList = rolePermissionDto.getRolePermission();
//
//        //查询是否存在用户
//        if(roleMapper2.getRoleById(roleId) == null){
//            throw new CommonException("查询用户不存在");
//        }
//        //查询用户权限，按照roleId
////        List<Integer> sqlPermissionList = permissionMapper.getListByRoleId(roleId);
//
//        //TODO 更新role表中role的数据变化
//
//        //删除角色的权限
//        permissionMapper2.deleteRolePermission(roleId);
//        //保存新的用户权限数据
//        for(Integer permission_id : rolePermissionList){
//            permissionMapper2.addRolePermission(roleId, permission_id);
//        }
//        return Result.success("更新成功");
//    }
//
//    private void buildPermissionTree(List<PermissionTreeVo> permissionTreeVoList, List<PermissionTreeVo> permissionTreeVos) {
//        for(PermissionTreeVo parent : permissionTreeVoList) {
//            List<PermissionTreeVo> children = permissionTreeVos.stream()
//                    .filter(permissionTreeVo -> Objects.equals(permissionTreeVo.getParentId(), parent.getId()))
//                    .collect(Collectors.toList());
//            parent.setChildren(children);
//            buildPermissionTree(children, permissionTreeVos);
//        }
//    }
//    private void buildMenuTree(List<Permission> parentList, List<Permission> permissions) {
//        for(Permission parent : parentList) {
//            List<Permission> children = permissions.stream()
//                    .filter(permission -> Objects.equals(permission.getParentId(), parent.getId()))
//                    .collect(Collectors.toList());
//            parent.setChildrenList(children);
//            buildMenuTree(children, permissions);
////            parent.setChildrenList(children);
////            processedChildren.addAll(children);
////            parent.setChildrenList(new ArrayList<>(children));
//
//        }
////        permissions.removeAll(processedChildren);
////        buildMenuTree(new ArrayList<>(processedChildren), permissions);
//    }
//
//    public List<Permission> getUserPermission(Integer userId){
//        return permissionMapper2.getList(userId);
//    }
//}
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
////    public List<String> getList(){
////        return permissionMapper.getList();
////    }
//    /*public void load() {
//
//        //获取权限列表
//        List<String> expressions = getList();
//        Map<String, Object> map = applicationContext.getBeansWithAnnotation(RestController.class);
////        System.out.println(map);
//        Collection<Object> controllers = map.values();
//        for(Object controller : controllers) {
//            Method[] methods = controller.getClass().getMethods();
//            System.out.println(methods);
//
//            for(Method method : methods){
////                System.out.println(method);
//                Authority annotation = method.getAnnotation(Authority.class);
////                System.out.println(annotation);
//                if(annotation != null){
//
//                    String name = annotation.name();
//                    String expression = annotation.expression();
//                    if(!expressions.contains(expression)){
//                        Permission permission = new Permission();
//                        permission.setName(name);
//                        permission.setDescription(expression);
//
//                        //保存到数据库
//                        permissionMapper.save(permission);
//                    }
//
//
//                }
//            }
//        }
//    }*/
//
//    /*//通过处理器映射器去拿到HandlerMethod， 其中会封装方法信息，在启动的时候就会装载方法
//    public void load2(){
//        //获取权限列表
//        List<String> expressions = getList();
//
//        Map<RequestMappingInfo, HandlerMethod> handlerMethods = requestMappingHandlerMapping.getHandlerMethods();
////        System.out.println(handlerMethods);
//        Collection<HandlerMethod> methods = handlerMethods.values();
//        for (HandlerMethod handlerMethod:
//             methods) {
////            System.out.println(handlerMethod);
//            Method method = handlerMethod.getMethod();
//            Authority annotation = method.getAnnotation(Authority.class);
////                System.out.println(annotation);
//            if(annotation != null){
//
//                String name = annotation.name();
//                String expression = annotation.expression();
//                if(!expressions.contains(expression)){
//                    Permission permission = new Permission();
//                    permission.setName(name);
//                    permission.setDescription(expression);
//
//                    //保存到数据库
//                    permissionMapper.save(permission);
//                }
//            }
//        }
//    }*/